/*
 * Copyright (C) 2020 UNISOC Open Source Project
 *
 */

package com.unisoc.systemui.qs.tiles;

import static android.view.WindowManager.ScreenshotSource.SCREENSHOT_GLOBAL_ACTIONS;
import static android.view.WindowManager.TAKE_SCREENSHOT_FULLSCREEN;

import android.content.Intent;
import android.os.Handler;
import android.util.Log;
import android.view.WindowManager;
import android.widget.Switch;

import com.android.internal.logging.MetricsLogger;
import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
import com.android.internal.util.ScreenshotHelper;
import com.android.systemui.R;
import com.android.systemui.plugins.qs.QSTile.BooleanState;
import com.android.systemui.qs.QSHost;
import com.android.systemui.qs.tileimpl.QSTileImpl;
import com.android.systemui.qs.QSTileHost;
import android.app.PerformanceManagerInternal;
import com.unisoc.systemui.screenshot.CommonUtils;

import javax.inject.Inject;

/** Quick settings tile: Control Screenshot **/
public class ScreenshotTile extends QSTileImpl<BooleanState> {

    public static final String TILE_NAME = "screenshot";

    private final Icon mIcon = ResourceIcon.get(com.android.internal.R.drawable.ic_screenshot);

    private final ScreenshotHelper mScreenshotHelper;

    @Inject
    public ScreenshotTile(QSHost host) {
        super(host);
        mScreenshotHelper = new ScreenshotHelper(mContext);
    }

    @Override
    protected void handleDestroy() {
        super.handleDestroy();
    }

    @Override
    public BooleanState newTileState() {
        return new BooleanState();
    }

    @Override
    public void handleSetListening(boolean listening) {
        super.handleSetListening(listening);
    }

    @Override
    protected void handleUserSwitch(int newUserId) {
    }

    @Override
    public Intent getLongClickIntent() {
        return null;
    }

    @Override
    public boolean isAvailable() {
        return true;
    }

    @Override
    protected void handleClick() {
        // Unisoc: Fix for bug 1073630
        if (PerformanceManagerInternal.getDefault().isUserAMonkeyNoCheck()) {
            return;
        }
        MetricsLogger.action(mContext, getMetricsCategory(), !mState.value);
        mHost.collapsePanels();
        mHandler.removeCallbacks(mScreenshotRunnable);
        mHandler.postDelayed(mScreenshotRunnable, 500);
    }

    @Override
    public CharSequence getTileLabel() {
        return mContext.getString(R.string.global_action_screenshot);
    }

    @Override
    protected void handleLongClick() {
    }

    @Override
    protected void handleUpdateState(BooleanState state, Object arg) {
        state.label = mHost.getContext().getString(R.string.global_action_screenshot);
        state.icon = mIcon;
        state.contentDescription = mContext.getString(R.string.global_action_screenshot);
        state.expandedAccessibilityClassName = Switch.class.getName();
    }

    @Override
    public int getMetricsCategory() {
        return MetricsEvent.ACTION_SCREENSHOT_POWER_MENU;
    }

    @Override
    protected String composeChangeAnnouncement() {
        return mContext.getString(R.string.global_action_screenshot);
    }

    final Runnable mScreenshotRunnable = new Runnable() {
        @Override
        public void run() {
            mScreenshotHelper.takeScreenshot(TAKE_SCREENSHOT_FULLSCREEN, true, true,
                    SCREENSHOT_GLOBAL_ACTIONS, mHandler, null);
        }
    };
}
